package com.sk.bilibili.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sk.bilibili.other.R;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import com.sk.bilibili.bean.Image;
import com.sk.bilibili.mapper.ImageMapper;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 邵康
 * @date 2021/12/04 14:53
 */
@Service
public class ToolService extends ServiceImpl<ImageMapper, Image> {

    @Autowired
    private MinioService minioService;
    private static final String RES_PATH = "res/%s.jpg";
    private static final Pattern RES_PATTERN = Pattern.compile("/(res/.*jpg)");

    public R<Void> deleteRes(Integer id) {
        Image temp = getById(id);
        minioService.delete(temp.getUrl(), RES_PATTERN);
        removeById(id);
        return R.success();
    }

    public R<List<Image>> uploadRes(MultipartFile[] files) {
        List<Image> list=new ArrayList<>();
        for (MultipartFile file:files){
            Image image = new Image();
            image.setTitle(file.getOriginalFilename());
            image.setUrl(minioService.add(file, RES_PATH));
            image.setUploadDate(new Date());
            save(image);
            list.add(image);
        }
        return R.success(list);
    }

    public R<List<Image>> getAll() {
        return R.success(list(null));
    }

}
